package com.yhg;

import com.alibaba.cloud.nacos.NacosConfigManager;
import com.alibaba.nacos.api.config.ConfigService;
import com.alibaba.nacos.api.config.listener.Listener;
import com.alibaba.nacos.client.env.NacosClientProperties;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean;

import java.util.concurrent.Executor;
import java.util.concurrent.Executors;

@EnableDiscoveryClient //服务发现
@EnableFeignClients //消费端（远程调用客户端，注解式rest）
@SpringBootApplication
public class OrderMainApplication {
    public static void main(String[] args) {
        SpringApplication.run(OrderMainApplication.class,args);
    }

    //1. 项目启动就监听配置文件变化
    @Bean
    ApplicationRunner applicationRunner(NacosConfigManager nacosConfigManager){  //从容器内自动拿NacosConfigManager
        return args -> {
            //2. 发生变化后拿到变化值
            ConfigService configService = nacosConfigManager.getConfigService();
            configService.addListener("service-order.properties",
                    "DEFAULT_GROUP",
                    new Listener() {
                        @Override
                        //线程池
                        public Executor getExecutor() {
                            return Executors.newFixedThreadPool( 4);
                        }

                        @Override
                        //接受配置信息
                        public void receiveConfigInfo(String configInfo) {
                            System.out.println("变化的配置信息："+configInfo);
                            System.out.println(
                                    "模拟邮件通知。。。"
                            );

                        }
                    });
            System.out.println("========");
        };
    }

    //3. 发送邮件

}